import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:loxen/mode/order_detail_mode.dart';
import 'package:loxen/routes/routes.dart';
import 'package:loxen/utils/color.dart';
import 'package:loxen/utils/config.dart';
import 'package:loxen/utils/image_url.dart';
import 'package:loxen/utils/time_util.dart';
import 'package:loxen/view/material/material_order_receive/order_detail_widget.dart';
import 'package:loxen/view/mine/staff_order_list_widget.dart';
import 'package:loxen/viewmode/mine_page_viewmodel.dart';
import 'package:provider/provider.dart';

class MineStaffWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var model = Provider.of<MinePageViewModel>(context, listen: false);
    var orders = model.listOrder;
    var staffInfo = model.staffInfo;

    List<Widget> list = [
      Padding(
        padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 30.h),
        child: Row(
          children: [
            Text(
              '日程',
              style: TextStyle(
                fontSize: 32.sp,
                color: Colours.main_text,
                fontWeight: FontWeight.bold,
              ),
            ),
            Expanded(child: Constants.empty),
            InkWell(
              onTap: () {
                Navigator.of(context)
                    .push(MaterialPageRoute(builder: (_) => StaffOrderListWidget()));
              },
              child: Text(
                '查看全部',
                style: TextStyle(
                  fontSize: 24.sp,
                  color: Colours.main_text,
                ),
              ),
            )
          ],
        ),
      )
    ];
    if (orders != null && orders.length > 0) {
      if (orders.length == 2) {
        list.add(_itemWidget(orders[0], context));
        list.add(Divider(height: 1.h));
        list.add(_itemWidget(orders[1], context));
      } else {
        list.add(_itemWidget(orders[0], context));
      }
    } else {
      list.add(Image.asset(
        'images/null_icon.png',
        width: 237.w,
        height: 152.w,
      ));
      list.add(Padding(
        padding: EdgeInsets.only(top: 20.h, bottom: 20.h),
        child: Text(
          '没有更多日程安排',
          style: TextStyle(
            fontSize: 28.sp,
            color: Colours.dark_text_gray,
          ),
        ),
      ));
    }
    return Scaffold(
      body: RefreshIndicator(
        onRefresh: () async {
          Provider.of<MinePageViewModel>(context, listen: false).loadData();
          return;
        },
        child: MediaQuery.removePadding(
          context: context,
          removeTop: true,
          child: ListView(
            shrinkWrap: true,
            children: [
              Container(
                height: 470.h,
                child: Stack(
                  children: [
                    Image.asset(
                      'images/staff_bg.png',
                      width: double.infinity,
                      height: double.infinity,
                      fit: BoxFit.cover,
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: 70.h, right: 50.w),
                      child: Row(
                        children: [
                          Expanded(child: Constants.empty),
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 155.h, left: 50.w),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          ClipRRect(
                            borderRadius: BorderRadius.circular(160.h),
                            child: CachedNetworkImage(
                              imageUrl:
                                  ImageUrlUtil.getClean(model?.data?.user?.portrait ?? "", w: 150),
                              height: 160.h,
                              width: 160.h,
                              fit: BoxFit.cover,
                            ),
                          ),
                          Expanded(
                            child: Padding(
                              padding: EdgeInsets.only(left: 20.w),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Padding(
                                    padding: EdgeInsets.only(top: 20.h),
                                    child: Text(
                                      staffInfo.real_name,
                                      style: TextStyle(
                                        fontSize: 40.sp,
                                        color: Colors.white,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.only(top: 10.h),
                                    child: Text(
                                      staffInfo.service_centre,
                                      style: TextStyle(
                                        fontSize: 28.sp,
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.only(top: 5.h),
                                    child: Text(
                                      '客户数：${staffInfo.customer_num}',
                                      style: TextStyle(
                                        fontSize: 28.sp,
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.only(top: 20.h),
                                    child: Material(
                                      borderRadius: BorderRadius.all(Radius.circular(35.h)),
                                      color: Color(0x7F000000),
                                      child: InkWell(
                                        onTap: () {
                                          context.read<MinePageViewModel>().changeStaff();
                                        },
                                        borderRadius: BorderRadius.all(Radius.circular(35.w)),
                                        child: Container(
                                          width: 110.w,
                                          height: 35.h,
                                          child: Center(
                                            child: Text(
                                              '切换角色',
                                              style: TextStyle(
                                                fontSize: 16.sp,
                                                color: Color(0xFFFCAE08),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                  )
                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 50.h),
                child: Card(
                  elevation: 5,
                  child: Container(
                    child: Column(
                      children: list,
                    ),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 10.h),
                child: Card(
                  elevation: 5,
                  child: InkWell(
                    onTap: () => Navigator.of(context).pushNamed(Routes.mineSettings),
                    child: Container(
                      padding: EdgeInsets.only(left: 20.w, right: 20.w),
                      height: 100.h,
                      child: Row(
                        children: [
                          Expanded(
                            child: Text(
                              '设置',
                              style: TextStyle(
                                fontSize: 28.sp,
                                color: Colours.main_text,
                              ),
                            ),
                          ),
                          Image.asset(
                            'images/mine_right.png',
                            width: 30.w,
                            height: 40.h,
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget _itemWidget(OrderDetailMode orderDetailMode, BuildContext context) {
    String imagePath;
    if (orderDetailMode.mode == '1') {
      imagePath = 'images/tongcheng.png';
    } else if (orderDetailMode.mode == '2') {
      imagePath = 'images/daodian.png';
    } else {
      imagePath = 'images/kuaidi.png';
    }
    return InkWell(
      onTap: () => Navigator.of(context).push(MaterialPageRoute(
          builder: (_) => OrderDetailWidget(id: orderDetailMode.id.toString(), staff: true))),
      child: Container(
        margin: EdgeInsets.only(top: 30.h, bottom: 30.h),
        padding: EdgeInsets.only(left: 20.w, right: 20.w),
        height: 100.h,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Image.asset(
              imagePath,
              width: 80.h,
              height: 80.h,
            ),
            Expanded(
              child: Padding(
                padding: EdgeInsets.only(left: 20.w, top: 10.h),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      orderDetailMode.getMode(),
                      style: TextStyle(
                        fontSize: 28.sp,
                        color: Colours.main_text,
                      ),
                    ),
                    Text(
                      TimeUtil.getStringTime2(orderDetailMode.startTime, orderDetailMode.endTime),
                      style: TextStyle(
                        fontSize: 24.sp,
                        color: Colours.main_text,
                      ),
                    )
                  ],
                ),
              ),
            ),
            Container(
              child: Center(
                child: Row(
                  children: [
                    Text(
                      orderDetailMode.getState(),
                      style: TextStyle(
                        fontSize: 24.sp,
                        color: Colours.app_main,
                      ),
                    ),
                    Image.asset(
                      'images/mine_right.png',
                      width: 30.w,
                      height: 40.h,
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
